package com.atgouwu.mall.product.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.atgouwu.mall.common.utils.R;
import com.atgouwu.mall.product.entity.SkuImagesEntity;
import com.atgouwu.mall.product.entity.SpuInfoDescEntity;
import com.atgouwu.mall.product.feign.SeckillFeignService;
import com.atgouwu.mall.product.service.*;
import com.atgouwu.mall.product.vo.SeckillSkuVo;
import com.atgouwu.mall.product.vo.SkuItemSaleAttrVo;
import com.atgouwu.mall.product.vo.SkuItemVo;
import com.atgouwu.mall.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atgouwu.mall.common.utils.PageUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.atgouwu.mall.product.dao.SkuInfoDao;
import com.atgouwu.mall.product.entity.SkuInfoEntity;
import javax.annotation.Resource;

@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {
    @Resource
    private ThreadPoolExecutor executor;
    @Resource
    private SkuSaleAttrValueService skuSaleAttrValueService;
    @Resource
    private SpuInfoDescService spuInfoDescService;
    @Resource
    private AttrGroupService attrGroupService;
    @Resource
    private SkuImagesService skuImagesService;
    @Autowired
    private SeckillFeignService seckillFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Page<>(Long.valueOf(String.valueOf(params.get("page"))), Long.valueOf(String.valueOf(params.get("size")))),
                new QueryWrapper<SkuInfoEntity>()
        );
        return new PageUtils(page.getRecords(), (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
    }

    //获取商品详情
    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo skuItemVo = new SkuItemVo();
        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity info = this.getById(skuId); //获取sku基本信息pms_sku_info
            List<SkuImagesEntity> imagesEntities = skuImagesService.getImagesBySkuId(skuId); //获取sku图片信息pms_sku_images
            info.setSkuImageList(imagesEntities);
            skuItemVo.setSkuInfo(info);
            return info;
        }, executor);
        CompletableFuture<Void> saleAttrFuture = infoFuture.thenAcceptAsync((res) -> {
            List<SkuItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrBySpuId(res.getSpuId()); //获取spu销售属性组合
            skuItemVo.setSpuSaleAttrList(saleAttrVos);
        }, executor);
        CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync((res) -> {
            SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(res.getSpuId()); //获取spu介绍pms_spu_info_desc
            skuItemVo.setDesc(spuInfoDescEntity);
        }, executor);
        CompletableFuture<Void> baseAttrFuture = infoFuture.thenAcceptAsync((res) -> {
            List<SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId()); //获取spu规格参数信息
            skuItemVo.setCategoryView(attrGroupVos);
        }, executor);
        CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> imagesEntities = skuImagesService.getImagesBySkuId(skuId); //sku图片信息pms_sku_images
            skuItemVo.setImages(imagesEntities);
        }, executor);
        CompletableFuture<Void> seckillFuture = CompletableFuture.runAsync(() -> {
            R skuSeckilInfo = seckillFeignService.getSkuSeckilInfo(skuId); //获取当前sku是否参与秒杀优惠活动(远程调用seckill秒杀服务)
            if (skuSeckilInfo.getCode() == 200) {
                SeckillSkuVo seckilInfoData = skuSeckilInfo.getData("data", new TypeReference<SeckillSkuVo>() {
                });
                skuItemVo.setSeckillSkuVo(seckilInfoData);
                if (seckilInfoData != null) {
                    long currentTime = System.currentTimeMillis();
                    if (currentTime > seckilInfoData.getEndTime()) {
                        skuItemVo.setSeckillSkuVo(null);
                    }
                }
            }
        }, executor);
        CompletableFuture.allOf(saleAttrFuture, descFuture, baseAttrFuture, imageFuture, seckillFuture).get(); //等到所有任务都完成
        return skuItemVo;
    }

    //获取banner轮播图
    @Override
    public List<SkuInfoEntity> bannerList(String sortField, Integer limit) {
        List<SkuInfoEntity> bannerList = baseMapper.selectList(new QueryWrapper<SkuInfoEntity>().orderByAsc(sortField).last("limit 0," + limit));
        return bannerList;
    }

    //获取今日推荐
    @Override
    public List<SkuInfoEntity> todayRecommend(String sortField, Integer limit) {
        List<SkuInfoEntity> todayRecommend = baseMapper.selectList(new QueryWrapper<SkuInfoEntity>().orderByAsc(sortField).last("limit 0," + limit));
        return todayRecommend;
    }

    //获取热卖排行,特价排行,新品排行
    @Override
    public List<SkuInfoEntity> hotSellList(String rankType, Integer limit) {
        List<SkuInfoEntity> hotSellList = baseMapper.selectList(new QueryWrapper<SkuInfoEntity>().orderByAsc(rankType).last("limit 0," + limit));
        return hotSellList;
    }

    //获取猜你喜欢
    @Override
    public List<SkuInfoEntity> likeList(String sortField, Integer limit) {
        List<SkuInfoEntity> likeList = baseMapper.selectList(new QueryWrapper<SkuInfoEntity>().orderByAsc(sortField).last("limit 0," + limit));
        return likeList;
    }

    //获取家用电器,手机通讯
    @Override
    public List<SkuInfoEntity> floorList(List<Long> categoryIds, Integer limit) {
        List<SkuInfoEntity> floorList = baseMapper.selectList(new QueryWrapper<SkuInfoEntity>().in("catalog_id", categoryIds).last("limit 0," + limit));
        return floorList;
    }

    //获取家用电器,手机通讯
    @Override
    public List<Map<String, Object>> floorList(String categoryIds, Integer limit) {
        List<Map<String, Object>> floorList = new ArrayList<>();
        List<SkuInfoEntity> SkuInfos = baseMapper.selectList(new QueryWrapper<SkuInfoEntity>().eq("catalog_id", categoryIds.split(",")[0]).last("limit 0," + limit).orderByDesc("sku_id"));
        Map<String, Object> floorMap = new HashMap<>();
        floorMap.put("id", 1);
        floorMap.put("name", "家用电器");
        floorMap.put("imgUrl", "http://182.92.178.182/static/floor-1-2.png");
        floorMap.put("bigImg", "http://182.92.178.182/static/floor-1-1.png");
        floorMap.put("carouselList", SkuInfos);
        List<Map<String, String>> navList = new ArrayList<>();
        Map<String, String> navMap = new HashMap<>();
        navMap.put("text", "热门");
        navMap.put("url", "#");
        navList.add(navMap);
        Map<String, String> navMaps = new HashMap<>();
        navMaps.put("text", "家用");
        navMaps.put("url", "#");
        navList.add(navMaps);
        floorMap.put("navList", navList);
        List<String> keywords = new ArrayList<>();
        keywords.add("节能补贴");
        keywords.add("4K电视");
        keywords.add("智能沙发");
        floorMap.put("keywords", keywords);
        List<String> recommendList = new ArrayList<>();
        recommendList.add("http://182.92.178.182/static/floor-1-3.png");
        recommendList.add("http://182.92.178.182/static/floor-1-b01.png");
        recommendList.add("http://182.92.178.182/static/floor-1-b02.png");
        recommendList.add("http://182.92.178.182/static/floor-1-b03.png");
        floorMap.put("recommendList", recommendList);
        floorList.add(floorMap);

        List<SkuInfoEntity> SkuInfoss = baseMapper.selectList(new QueryWrapper<SkuInfoEntity>().eq("catalog_id", categoryIds.split(",")[1]).last("limit 0," + limit).orderByDesc("sku_id"));
        Map<String, Object> floorMaps = new HashMap<>();
        floorMaps.put("id", 2);
        floorMaps.put("name", "手机通讯");
        floorMaps.put("imgUrl", "http://182.92.178.182/static/floor-1-1.png");
        floorMaps.put("bigImg", "http://182.92.178.182/static/floor-1-3.png");
        floorMaps.put("carouselList", SkuInfoss);
        List<Map<String, String>> navLists = new ArrayList<>();
        Map<String, String> navMapss = new HashMap<>();
        navMapss.put("text", "热门");
        navMapss.put("url", "#");
        navLists.add(navMapss);
        Map<String, String> navMapsss = new HashMap<>();
        navMapsss.put("text", "手机");
        navMapsss.put("url", "#");
        navLists.add(navMapsss);
        floorMaps.put("navList", navLists);
        List<String> keywordss = new ArrayList<>();
        keywordss.add("手机补贴");
        keywordss.add("5G通讯");
        keywordss.add("智能手机");
        floorMaps.put("keywords", keywordss);
        List<String> recommendLists = new ArrayList<>();
        recommendLists.add("http://182.92.178.182/static/floor-1-5.png");
        recommendLists.add("http://182.92.178.182/static/floor-1-b03.png");
        recommendLists.add("http://182.92.178.182/static/floor-1-b01.png");
        recommendLists.add("http://182.92.178.182/static/floor-1-b02.png");
        floorMaps.put("recommendList", recommendLists);
        floorList.add(floorMaps);
        return floorList;
    }

}